/*
 * Copyright (C) 2022 Huawei Technologies Co., Ltd.
 * Licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain longVal1 copy of Mulan PSL v2 at:
 * http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include <tee_ext_api.h>
#include <tee_log.h>
#include <securec.h>
#include <tee_arith_api.h>
#include <tee_mem_mgmt_api.h>
#include "test_arithmetic_api_base.h"

// p1,p2 and p3 are primes
static uint8_t g_p1Value[] = {
    0x55, 0xE6, 0xE0, 0xE3, 0x96, 0x50, 0x92, 0x4F, 0x8A, 0x53, 0x48, 0x83, 0xCA, 0xDD, 0x81, 0x2D,
    0x48, 0xBA, 0x7D, 0x71, 0x25, 0xDD, 0x54, 0xB1, 0x01, 0x71, 0x72, 0xFD, 0xC9, 0x11, 0xB4, 0x00,
    0x4B, 0x7C, 0xF4, 0x32, 0xEB, 0x20, 0x12, 0x8E, 0x8D, 0x9D, 0x02, 0x5A, 0xB9, 0xF9, 0xC2, 0xC2,
    0x1E, 0x41, 0x6A, 0xC3, 0xA9, 0x2B, 0x59, 0x21, 0x80, 0x7B, 0xF8, 0x23, 0x3A, 0x73, 0xBE, 0x87,
    0xF7, 0x91, 0xE6, 0xFC, 0x57, 0x15, 0x04, 0x78, 0xCA, 0xD2, 0x75, 0x6E, 0xD1, 0x4E, 0x83, 0x08,
    0x95, 0x60, 0x49, 0x97, 0xF0, 0x83, 0x74, 0xC8, 0xEF, 0x58, 0xFE, 0x0D, 0xAA, 0x4D, 0x81, 0x40,
    0xE0, 0x67, 0x60, 0x76, 0x71, 0xD7, 0xC2, 0x83, 0x7F, 0xF0, 0xC4, 0x1B, 0x2F, 0xCF, 0x3E, 0x3C,
    0x06, 0x07, 0x64, 0x40, 0xBA, 0x83, 0x3E, 0x80, 0x01, 0x9D, 0x52, 0xF6, 0x5F, 0xC5, 0xCF, 0xCB
};

static uint8_t g_p2Value[] = {
    0x54, 0x5D, 0x1C, 0xAA, 0x75, 0xF2, 0xCB, 0x78, 0x97, 0x5A, 0x4D, 0x41, 0xAE, 0x9A, 0xDD, 0xB5,
    0x62, 0x10, 0x1F, 0x86, 0x64, 0x07, 0x98, 0x2D, 0x92, 0x00, 0x62, 0x26, 0x35, 0xE9, 0x63, 0xD4,
    0x96, 0x0B, 0x35, 0x25, 0x93, 0xAD, 0x26, 0x0F, 0xD0, 0xE6, 0x86, 0x9F, 0xD3, 0x7C, 0xEC, 0xD5,
    0x6E, 0x23, 0x44, 0x6B, 0xE6, 0x2E, 0xC8, 0x03, 0x5C, 0xB9, 0x63, 0xDF, 0x40, 0xB3, 0xA0, 0x1C,
    0x32, 0xB8, 0x0D, 0x06, 0x04, 0x51, 0xA5, 0x12, 0x75, 0x46, 0x61, 0x8C, 0x65, 0xC2, 0x39, 0x56,
    0x67, 0x3E, 0x51, 0x49, 0xF5, 0xAB, 0x34, 0xEE, 0xE8, 0x35, 0xDA, 0xB8, 0xDC, 0x2A, 0x96, 0x32,
    0xB6, 0x70, 0xB3, 0x49, 0xC2, 0x67, 0xBB, 0x08, 0x89, 0x18, 0x81, 0xED, 0x0F, 0xBE, 0x8C, 0xC1,
    0x39, 0x70, 0x36, 0x61, 0x3E, 0xF7, 0xF9, 0x6C, 0xF3, 0xE1, 0x74, 0x14, 0xC0, 0x49, 0x05, 0x89
};

static uint8_t g_p3Value[] = {
    0x64, 0xDB, 0x43, 0xB6, 0x9E, 0xD9, 0x3A, 0x84, 0xFD, 0xA5, 0xD2, 0xB4, 0xC3, 0xB9, 0x0B, 0xAD,
    0x84, 0xF9, 0x30, 0x3F, 0xE1, 0xDE, 0x53, 0x3E, 0x20, 0x6E, 0x09, 0xD9, 0x59, 0x77, 0xC0, 0x8E,
    0x5D, 0xE4, 0x27, 0x15, 0xB6, 0x51, 0x98, 0x44, 0x71, 0x7E, 0x2B, 0x4E, 0x79, 0x27, 0x49, 0xAE,
    0x4C, 0xF5, 0xCC, 0x6F, 0xCF, 0xBF, 0x27, 0x7E, 0x07, 0x71, 0x4E, 0x48, 0x94, 0x7F, 0x60, 0xFD,
    0x32, 0x81, 0xFB, 0x44, 0x43, 0x04, 0xB1, 0xE0, 0xA2, 0x49, 0xEF, 0xAA, 0xC7, 0x8E, 0x9B, 0xC3,
    0x0C, 0x34, 0x33, 0xF4, 0x23, 0x84, 0x86, 0xAD, 0x70, 0xD9, 0x0F, 0xBE, 0xBC, 0x5A, 0xE6, 0x9E,
    0xC7, 0xB5, 0x15, 0x82, 0xE6, 0x17, 0x10, 0x06, 0x09, 0xC9, 0x9F, 0x9D, 0x99, 0xBA, 0x10, 0x63,
    0x27, 0x59, 0x46, 0xCF, 0x8B, 0x0C, 0x32, 0x0D, 0x0D, 0x9C, 0x82, 0x5A, 0x82, 0x17, 0x43, 0x03
};

static uint8_t g_vCheckValue[] = {
    0x10, 0x69, 0x66, 0x78, 0x0E, 0xAE, 0xE1, 0x39, 0x02, 0x7C, 0xCE, 0x5B, 0x8F, 0x7C, 0xE5, 0x30,
    0x77, 0xA2, 0xF7, 0xE6, 0x75, 0xE8, 0x26, 0xD8, 0xAA, 0x37, 0xEB, 0x71, 0xF3, 0x0D, 0x94, 0xC5,
    0x11, 0xE1, 0x98, 0xF3, 0x65, 0xAA, 0x28, 0xE2, 0xE7, 0x2B, 0x15, 0x2C, 0x63, 0x77, 0x64, 0x40,
    0x22, 0xA8, 0xE4, 0xA7, 0xF5, 0x4E, 0x84, 0x51, 0xDE, 0x0A, 0x3C, 0x2F, 0x81, 0xD2, 0xDF, 0xE2,
    0x6C, 0x20, 0x23, 0x71, 0xB9, 0xA0, 0x0D, 0x00, 0x2A, 0x17, 0x55, 0x1C, 0xB5, 0xA1, 0x66, 0xBD,
    0xDE, 0xAC, 0x9D, 0x32, 0x30, 0xF9, 0xFF, 0x02, 0x8F, 0xD5, 0xC1, 0x1D, 0x4C, 0x91, 0x40, 0x46,
    0x62, 0xA7, 0x8E, 0xAB, 0x6B, 0x74, 0x4E, 0x44, 0x02, 0x08, 0xA4, 0x3D, 0x8B, 0xAB, 0x61, 0xAE,
    0xEE, 0xDC, 0x23, 0x4C, 0x70, 0x7A, 0x31, 0x2B, 0x5C, 0xDD, 0x31, 0x7C, 0x2B, 0x57, 0x69, 0xA3
};

static uint8_t g_uCheckValue[] = {
    0x20, 0xb3, 0x43, 0x5f, 0x2a, 0x64, 0xb5, 0x6e, 0xd8, 0x63, 0x4e, 0xf3, 0xd4, 0xd3, 0x41, 0x1a,
    0xf1, 0x06, 0xb0, 0x28, 0xab, 0x05, 0x8d, 0x5b, 0xf5, 0xca, 0xa3, 0x1b, 0x90, 0x8d, 0x44, 0xae,
    0xd4, 0x26, 0x08, 0x6b, 0x1e, 0x65, 0xbf, 0xc9, 0x26, 0x46, 0xf2, 0xb6, 0xc4, 0xa7, 0x55, 0xdd,
    0x93, 0x42, 0xc0, 0xd4, 0x0c, 0xc9, 0x61, 0x74, 0xd6, 0x05, 0x45, 0x25, 0x70, 0x69, 0xc8, 0x7a,
    0x7c, 0x05, 0xec, 0xf1, 0xe3, 0x51, 0xf3, 0xb0, 0x8d, 0x6b, 0xbd, 0x8a, 0x57, 0xb0, 0x6c, 0x37,
    0x15, 0x56, 0x93, 0x90, 0x74, 0x6b, 0x88, 0x12, 0x51, 0x6c, 0xde, 0xd7, 0x98, 0x12, 0x6f, 0x02,
    0xca, 0x36, 0x63, 0xf5, 0x90, 0x87, 0x75, 0x95, 0x28, 0x9a, 0x6d, 0x19, 0xa0, 0xed, 0xa1, 0x53,
    0x6c, 0x71, 0xa4, 0x2b, 0x8d, 0xfc, 0x3f, 0xbb, 0xaa, 0xcb, 0x89, 0x24, 0xfd, 0xe8, 0x3e, 0x8a,
};

TEE_Result TestBigIntRelativePrime()
{
    tlogi("[%s] begin:", __FUNCTION__);
    TEE_Result ret = TEE_SUCCESS;
    TEE_BigInt *p1 = CreateBigInt(sizeof(g_p1Value), g_p1Value);
    TEE_BigInt *p2 = CreateBigInt(sizeof(g_p2Value), g_p2Value);
    TEE_BigInt *tmp1 = CreateBigInt(RESULT_SIZE, 0);
    TEE_BigInt *tmp2 = CreateBigInt(RESULT_SIZE, 0);

    TEE_BigIntConvertFromS32(tmp1, NUM_THREE);
    TEE_BigIntMul(tmp1, tmp1, p1); // tmp1 = p1 * 3
    TEE_BigIntConvertFromS32(tmp2, NUM_FIVE);
    TEE_BigIntMul(tmp2, tmp2, p2); // temp2 = p2 * 5
    if (!TEE_BigIntRelativePrime(tmp1, tmp2)) {
        tloge("BigIntRelativePrime fail.\n");
        ret = TEE_ERROR_GENERIC;
    }

    TEE_Free(p1);
    TEE_Free(p2);
    TEE_Free(tmp1);
    TEE_Free(tmp2);
    tlogi("[%s] end. ret = 0x%x.", __FUNCTION__, ret);
    return ret;
}

TEE_Result TestBigIntComputeExtentedGcd()
{
    tlogi("[%s] begin:", __FUNCTION__);
    TEE_Result ret = TEE_SUCCESS;

    TEE_BigInt *p1 = CreateBigInt(sizeof(g_p1Value), g_p1Value);
    TEE_BigInt *p2 = CreateBigInt(sizeof(g_p2Value), g_p2Value);
    TEE_BigInt *p3 = CreateBigInt(sizeof(g_p3Value), g_p3Value);
    TEE_BigInt *a = CreateBigInt(RESULT_SIZE, 0);
    TEE_BigInt *b = CreateBigInt(RESULT_SIZE, 0);
    TEE_BigInt *u = CreateBigInt(RESULT_SIZE, 0);
    TEE_BigInt *v = CreateBigInt(RESULT_SIZE, 0);
    TEE_BigInt *tmp1 = CreateBigInt(RESULT_SIZE * NUM_TWO, 0);
    TEE_BigInt *tmp2 = CreateBigInt(RESULT_SIZE * NUM_TWO, 0);
    TEE_BigInt *gcd = CreateBigInt(RESULT_SIZE, 0);

    // a = p1 * p2 * 3
    TEE_BigIntMul(a, p1, p2);
    TEE_BigIntConvertFromS32(tmp1, NUM_THREE);
    TEE_BigIntMul(a, a, tmp1);

    // b = p1 * p3 * 5
    TEE_BigIntMul(b, p1, p3);
    TEE_BigIntConvertFromS32(tmp2, NUM_FIVE);
    TEE_BigIntMul(b, b, tmp2);

    // gdc (a,b) = gdc ((p1*p2*3),(p1*p3*5)) = p1
    TEE_BigIntComputeExtendedGcd(gcd, u, v, a, b);
    if (TEE_BigIntCmp(gcd, p1) != 0) {
        tloge("BigIntComputeExtendedGcd check gcd fail.");
        ret = TEE_ERROR_GENERIC;
        goto CLEANUP;
    }

    TEE_BigInt *uCheck = CreateBigInt(sizeof(g_uCheckValue), g_uCheckValue);
    TEE_BigInt *vCheck = CreateBigInt(sizeof(g_vCheckValue), g_vCheckValue);
    TEE_BigIntNeg(vCheck, vCheck); // turn v to negative
    if (TEE_BigIntCmp(v, vCheck) != 0 || TEE_BigIntCmp(u, uCheck) != 0) {
        tloge("BigIntComputeExtendedGcd check v or u fail.");
        ret = TEE_ERROR_GENERIC;
    }
    TEE_Free(uCheck);
    TEE_Free(vCheck);

CLEANUP:
    TEE_Free(p1);
    TEE_Free(p2);
    TEE_Free(p3);
    TEE_Free(a);
    TEE_Free(b);
    TEE_Free(u);
    TEE_Free(v);
    TEE_Free(tmp1);
    TEE_Free(tmp2);
    TEE_Free(gcd);
    tlogi("[%s] end. ret = 0x%x.", __FUNCTION__, ret);
    return ret;
}

TEE_Result TestBigIntIsProbablePrime()
{
    tlogi("[%s] begin:", __FUNCTION__);
    TEE_Result ret = TEE_SUCCESS;
    const uint32_t leastConfidenceLevel = 80;
    // p1 is a prime
    uint8_t g_p1Value[] = {
        0x40, 0x84, 0xB4, 0xAF, 0xF9, 0x13, 0xE0, 0x2B, 0xFF, 0x2D, 0x30, 0x21, 0x7C, 0x94, 0xD2, 0x8F,
        0x89, 0x47, 0x9F, 0x7E, 0x14, 0xC1, 0xB4, 0xD7, 0x64, 0x97, 0x33, 0x65, 0x26, 0x62, 0x86, 0xED
    };
    TEE_BigInt *p1 = CreateBigInt(sizeof(g_p1Value), g_p1Value);
    TEE_BigInt *tmp = CreateBigInt(RESULT_SIZE, 0);

    if (!TEE_BigIntIsProbablePrime(p1, leastConfidenceLevel)) {
        tloge("check p1 is a prime fail.");
        ret = TEE_ERROR_GENERIC;
        goto CLEANUP;
    }
    TEE_BigIntConvertFromS32(tmp, NUM_THREE);
    TEE_BigIntMul(p1, p1, tmp);
    // check that p1 is not a prime
    if (TEE_BigIntIsProbablePrime(p1, leastConfidenceLevel)) {
        tloge("check p1 is not a prime fail.");
        ret = TEE_ERROR_GENERIC;
    }

CLEANUP:
    TEE_Free(p1);
    TEE_Free(tmp);
    tlogi("[%s] end. ret = 0x%x.", __FUNCTION__, ret);
    return ret;
}